<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 积分兑换ajax类
 * @package  	ajax
 * @subpackage  backend
 * @author   	create by allen.qiang
 */
quanli_require( 'module/base/promotionHandler.class.php' );
quanli_require( 'module/base/goodsHandler.class.php' );
class ajax_pointsMall {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //积分商城促销查询
                    $this->searchPointsMall();
                    break;
                    
                case 2: //添加或更新积分商城促销
                    $this->savePointsMall();
                    break;
                    
                case 3: //删除积分商城促销
                    $this->delPointsMall();
                    break;
                    
                case 4:  //积分商城促销 禁用、启用
                    $this->setPointsMallUseflg();
                    break;
                   
                case 5: //验证商品
                	$this->checkGoods();
                	break;
                	
                case 6: //验证优惠券
                	$this->checkCoupon();
                	break;
                
            }

        }

    }
	
    /**
     * 验证优惠有效性
     *
     */
    private function checkCoupon(){
    	global $G_PGLOB;
    	
    	$promotionHandler = new promotionHandler( $G_PGLOB->DBCA );
    	
    	$_REQUEST['sys_datatime'] = $G_PGLOB->sys_datetime;
    	
    	$result_array = $promotionHandler->checkPointsMallCoupon( $_REQUEST );

    	echo json_encode( $result_array );
    }
    /**
     * 验证添加商品
     *
     */
    private function checkGoods(){
    	
    	global $G_PGLOB;
    	
    	if (!isset($_REQUEST['product_code']) ) {
    		die( json_encode( array( 'code' => 400, 'message' => '请输入商品编码' ) ) );
    	}
    	
    	$goodsHandler = new goodsHandler( $G_PGLOB->DBCA );
    	$result_array = $goodsHandler->searchGoodsSale( $_REQUEST );
    	if (!$result_array) {
    		echo json_encode( array( 'code' => 400, 'message' => '没有查找到商品' ));
    	}
    	
    	$promotionHandler = new promotionHandler( $G_PGLOB->DBCA );
    	$points_goods_result = $promotionHandler->checkPointsMallGoods( $_REQUEST );
    	
    	if (isset($points_goods_result['code']) && $points_goods_result['code'] == 200) {
    		echo json_encode( array( 'code' => 200, 'message' => 'ok', 'data' => $result_array));
    	} else {
    		echo json_encode( array( 'code' => 400, 'message' => $points_goods_result['message']));
    	}
    	
    }
    
    /**
     * 积分商城促销查询
     */
    private function searchPointsMall() {

        global $G_PGLOB;
		
        //查询条件
        $promotionHandler = new promotionHandler( $G_PGLOB->DBCA ) ;
        $result_data	  = $promotionHandler->searchPointsMall( $_REQUEST ) ;
		
        //查询结果
        $result_array = array(
            'page'  => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows'  => array()
        );
		
        if ($result_data) {
            foreach($result_data as &$p_c) {
				
            	if ( $p_c['type'] == 1 ) { 
            		$typeName = "良票换券";
            	} else if ( $p_c['type'] == 2 ) {
            		$typeName = "良票兑换";
            	} else if ( $p_c['type'] == 3 ) {
            		$typeName = "良票换购";
            	}
            	
                $result_array['rows'][] = array(
                    'id' 	=> $p_c['promotionID'],
                    'cell' 	=> array(
                        '<a href="javascript: void(0);" onclick="promotion.edit(' . $p_c['promotionID'] . ')">修改</a>&nbsp;' ,
                        $p_c['promotionID'] ,
                        $p_c['promotionName'],
                        $p_c['promotionIndex'],
                        $typeName,
                        $p_c['startDate'],
                        $p_c['endDate'],
                        $p_c['remark']
                        //$p_c['useFlg'] == 1 ? '<a href="javascript: void(0);" onclick="promotion.changeConfirm(' . $p_c['promotionID'] . ', 1) " >启用</a>' :
                            //'<a href="javascript: void(0);" onclick="promotion.changeConfirm(' . $p_c['promotionID'] . ', 0 ) " >禁用</a>'
                    )
                );
            }
        }

        echo json_encode( $result_array );
    }

    /**
     * 添加或更新积分商城促销
     *
     */
    private function savePointsMall () {
        global $G_PGLOB;

        $_REQUEST['creator'] = $G_PGLOB->currentUserID;
		
        $promotionHandler 	 = new promotionHandler( $G_PGLOB->DBCA ) ;
        $result_array 		 = $promotionHandler->savePointsMall( $_REQUEST ) ;
		
        echo json_encode($result_array);
    }

    /**
     * 删除积分商城促销
     *
     */
    private function delPointsMall () {

        global $G_PGLOB;
        
        if (!isset($_REQUEST['delArr']) || empty($_REQUEST['delArr'])) {
        	die( json_encode( array( 'code' => 400, 'message' => '请选择删除促销' ) ) );
        }
		
        $del_array = $_REQUEST['delArr'];
		
        try{
        	
			//开启事务
            $G_PGLOB->DBCA->beginTransaction();
            
        	foreach ($del_array as $promotionID) {
        		
        		//查找促销信息是否存在
        		$pointsMall = new entity('promotion_pointsmall');
        		$pointsMall->promotionID = $promotionID;
        		if (!$G_PGLOB->DBCA->fetch($pointsMall)) {
        			throw new Exception('促销活动未找到', -1000);
        		}
        		
        		$G_PGLOB->DBCA->delete($pointsMall);
        		
        		$sql = 'DELETE FROM promotion_pointsmall_detail WHERE promotionID = ' . $promotionID;
        		
        		$G_PGLOB->DBCA->execute($sql);
        		
        	}
            
            //提交事务
            $G_PGLOB->DBCA->commit();

            echo json_encode( array('message'=>'删除成功', 'code'=>200) );

        }catch( Exception $e ){
        	
			//回滚
            $G_PGLOB->DBCA->rollBack();
            
            if ($e->getCode() < 0) {
            	die( json_encode( array( 'code'=> $e->getCode(), 'message'=>'系统错误，请与管理员联系') ) );
            } else {
	            die( json_encode( array( 'code'=> 500 , 'message'=>'系统错误，请与管理员联系') )  );
            }
            

        }
			
    }

    /**
     * 积分商城促销 禁用、启用
     */
    private function setPointsMallUseflg () {

        global $G_PGLOB;
        
        if (!isset($_REQUEST['promotionID']) || !is_numeric($_REQUEST['promotionID'])) {
        	die( json_encode( array( 'code' => 400, 'message' => '请选择操作促销' ) ) );
        }
        
        if (!isset($_REQUEST['flg']) || !in_array($_REQUEST['flg'],array(0,1))) {
        	die( json_encode( array( 'code' => 400, 'message' => '参数错误' ) ) );
        }
		
        $promotionID = $_REQUEST['promotionID'];
        $flg   = $_REQUEST['flg'];

        $pointsmall = new entity('promotion_pointsmall');
        $pointsmall->promotionID = $promotionID;
        if (!$G_PGLOB->DBCA->fetch($pointsmall)) {
        	die( json_encode( array( 'code' => 400, 'message' => '促销不存在' ) ) );
        }
        
        if ($flg == '1' && $pointsmall->useFlg == '0') {
        	die( json_encode( array( 'code' => 400, 'message' => '该促销已经禁用' ) ) );
        }
        
        if ($flg == '0' && $pointsmall->useFlg == '1') {
        	die( json_encode( array( 'code' => 400, 'message' => '该促销已经启用' ) ) );
        }
        
        if ($flg == 1) {
        	$pointsmall->useFlg = 0;
        } else {
        	$pointsmall->useFlg = 1;
        }
		        
        try {
        	
        	$G_PGLOB->DBCA->save($pointsmall);
        	
        	if ($flg == '1') {
        		die( json_encode( array( 'code' => 200, 'message' => '禁用成功' ) ) );
        	} else {
        		die( json_encode( array( 'code' => 200, 'message' => '启用成功' ) ) );
        	}
        	
        } catch (Exception $e) {
        	
        	error_log( $e->getMessage() );
            die( json_encode( array('message'=>'系统错误，请与管理员联系' , 'code'=>500 ) ) );
        }
		
    }

}